/**
 * 
 */
package com.dalonedrau.jogl.opengl.ui;

import java.awt.Point;

import com.dalonedrau.jogl.engine.Action;
import com.dalonedrau.jogl.engine.Interface;
import com.dalonedrau.jogl.engine.ProjectConstants;
import com.dalonedrau.jogl.opengl.ui.border.EmptyBorder;
import com.dalonedrau.jogl.opengl.ui.border.TextBorder;
import com.dalonedrau.jogl.opengl.ui.panel.Panel;
import com.dalonedrau.jogl.opengl.ui.panel.PortraitPagePanel;
import com.dalonedrau.jogl.util.UtilityMethods;

/**
 * @author Donald
 */
public class SmallMessageViewer extends PortraitPagePanel {
	/** the singleton instance of {@link SmallMessageViewer}. */
	private static SmallMessageViewer	instance;
	/**
	 * Gets the singleton instance of {@link SmallMessageViewer}.
	 * @return {@link SmallMessageViewer}
	 */
	public static SmallMessageViewer getInstance() {
		if (SmallMessageViewer.instance == null) {
			SmallMessageViewer.instance = new SmallMessageViewer();
		}
		return SmallMessageViewer.instance;
	}
	/** the dialog's border. */
	private TextBorder	border;
	/** the okay button. */
	private Button		btnOkay;
	/** the message label. */
	private Label		lblMessage;
	/** Creates a new instance of {@link SmallMessageViewer}. */
	private SmallMessageViewer() {
		super(Panel.PORTRAIT_CENTER);
		lblMessage = new Label(ProjectConstants.getInstance().getFontH2(), "");
		lblMessage.setBorder(new EmptyBorder(0, 0, 3, 0));
		lblMessage.setMaximumWidth(400);
		super.addChild(lblMessage);
		btnOkay =
				new Button(ProjectConstants.getInstance().getFontH1(), "Okay");
		btnOkay.addAction(new Action() {
			/**
			 * {@inheritDoc}
			 */
			@Override
			public void performAction() {
				Interface.getInstance().hideSmallMessageWindow();
			}
		});
		super.addChild(btnOkay);
		border = new TextBorder();
		super.setBorder(border);
	}
	/**
	 * Fires the same actions as a mouse click when the [Enter] key is pressed.
	 */
	public void fireMouseClickThruEnter() {
		btnOkay.performAction();
	}
	/**
	 * Shows the message window.
	 * @param header the message title
	 * @param msg the message
	 */
	public void showMessage(final String header, final String msg) {
		this.showMessage(header, msg, null);
	}
	/**
	 * Shows the message window.
	 * @param header the message title
	 * @param msg the message
	 * @param e the callback event
	 */
	public void showMessage(final String header, final String msg,
			final Action e) {
		border.setText(header);
		lblMessage.setText(msg);
		if (e != null) {
			btnOkay.addAction(e);
		}
		btnOkay.addAction(new Action() {
			/**
			 * {@inheritDoc}
			 */
			@Override
			public void performAction() {
				Interface.getInstance().hideSmallOptionsMessageWindow();
			}
		});
		super.setPreferredSize(0, 0);
		super.getPreferredSize();
		Point pt = UtilityMethods.getInstance().centerComponentInFrame(this);
		setPosition(pt.x, pt.y, 10000f);
		setVisible(true);
	}
}
